package com.hejjon.schedule;

import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * Created by caoshi at 6:36 2021-08-22
 */
@Configuration
public class SaveScheduleConfig {

    @Autowired
    private Scheduler scheduler;

    @PostConstruct
    public void jobConf() {
        // 设置任务调度器, 每1分钟执行一次任务
        CronScheduleBuilder builder = CronScheduleBuilder.cronSchedule("0 0/1 * * * ? *");
        JobDetail jobDetail = JobBuilder.newJob(SaveStudent.class)
                .withIdentity("testName", "testGroup")
                .build();
        CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("testName", "testGroup")
                .withSchedule(builder).build();

        try {
            if (this.scheduler.checkExists(jobDetail.getKey())) {
                this.scheduler.deleteJob(jobDetail.getKey());
            }
            this.scheduler.scheduleJob(jobDetail, trigger);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }

    }
}
